﻿using MinorAspNetCore.Server;
using System;
using System.Collections.Generic;

namespace MinorAspNetCore
{
    public interface IMinorWebHostBuilder
    {
        IMinorWebHostBuilder UseServer(IServer server);

        IMinorWebHostBuilder Configure(Action<IMinorApplicationBuilder> configure);

        IMinorWebHost Build();
    }

    public class MinorWebHostBuilder : IMinorWebHostBuilder
    {
        private IServer _server;
        private readonly List<Action<IMinorApplicationBuilder>> _configures = new List<Action<IMinorApplicationBuilder>>();

        public IMinorWebHostBuilder Configure(Action<IMinorApplicationBuilder> configure)
        {
            _configures.Add(configure);
            return this;
        }

        public IMinorWebHostBuilder UseServer(IServer server)
        {
            _server = server;
            return this;
        }

        public IMinorWebHost Build()
        {
            var builder = new MinorApplicationBuilder();
            foreach (var configure in _configures)
            {
                configure(builder);
            }
            return new MinorWebHost(_server, builder.Build());
        }
    }
}